var async = require("async");
var renderEngin = require("../common/renderEngin");
var Tools = require("../common/tools");
exports.product_list = function(req, res) {
    use(["public/javascripts/product_list/init.js", "public/javascripts/service/product_listService.js"], function(PageComponent, product_listService) {
        var storage = {};
        storage["slider"] = Tools.getPageSliderData("user_center");
        storage["parentid"] = req.query.parentid || '';
        storage["childid"] = req.query.childid || '';

        //获取菜单
        /**
         * 获取一级菜单
         */
        product_listService.getGroups({headers: req.headers, urlParams: {'parentId': 0}}, function (result) {
            if (result.errorCode == 0) {
                storage['bigtype'] = result.data;
            }
            if (req.query.parentid) {
                product_listService.getGroups({
                    headers: req.headers,
                    urlParams: {'parentId': req.query.parentid}
                }, function (results) {
                    if (results.errorCode == 0) {
                        storage['smalltype'] = results.data;
                        getList();
                    }
                });
            } else {
                getList();
            }
        });

        function getList() {
            /**
             * 获取列表
             */
            var topGrade = req.query.parentid ? req.query.parentid : 0,
                secondGrade = req.query.childid ? req.query.childid : '';
            var config = {
                headers: req.headers,
                urlParams: {
                    'topGrade': topGrade,
                    'secondGrade': secondGrade
                }
            };

            product_listService.getList(config, function (result) {
                if (result.errorCode == 0) {
                    storage['list'] = result.data;
                    storage['totalResult'] = result.pagination.totalResult;
                    PageComponent = React.createFactory(PageComponent);
                    renderEngin.call(res, 'product_list', PageComponent, storage);
                }
            });
        }
    });
}